package com.ambimmort.shj.service.dpi.users.group.brasip;

import cn.net.greenet.common.policy_service.PolicySupport;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Dpiusergroup;
import cn.net.greenet.db.bean.UgBrasip;
import com.ambimmort.umessage.UcType;
import com.ambimmort.umessage.uc.outgoing.vm.x40;
import com.ambimmort.umessage.util.HexUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * Created by Administrator on 2015/7/30.
 */
@Service("brasipDeleteService")
public class DeleteService {

    @Autowired
    private SessionFactory hibernateSessionFactory;
    /**
     *
     * @param json
     * @throws Throwable
     */
    public void brasipDelete(String json) throws Throwable{
        JSONObject jsonObj = JSONObject.fromObject(json);
        String groupId = jsonObj.getString("groupId");
        String instanceName = jsonObj.getString("instanceName");
        JSONArray brasips = jsonObj.getJSONArray("ids");
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();
        java.util.List<x40> msgs = new ArrayList<x40>();
        x40.User user = null;
        x40 msg = null;
        Dpiusergroup groupBean = (Dpiusergroup) session.get(Dpiusergroup.class, Integer.parseInt(groupId));

        try {
            UgBrasip ptr = null;
            Object objPtr = null;
            for (int i = 0; i < brasips.size(); i++) {
                objPtr = session.get(UgBrasip.class, brasips.getInt(i));
                if (objPtr == null) {
                    continue;
                }
                ptr = (UgBrasip) objPtr;
                session.delete(ptr);

                if ((i) % 2000 == 0) {
                    msg = new x40();
                    msg.getHeader().setMessageNo(UcType.newUINT4(0));
                    msg.getHeader().setMessageSequenceNo(UcType.newUINT4(0));
                    msg.getHeader().setMessageType(new UcType.UINT1(new byte[]{(byte) 0x40}));
                    msg.setAction(UcType.newUINT1(2));
                    msg.setUserGroupName(UcType.newUString_UINT1(groupBean.getName()));
                    msg.setUserGroupNo(UcType.newUINT2(groupBean.getId()));
                    msg.setMessageSerialNo(UcType.newUINT4(0));
                    msgs.add(msg);
                }
                user = new x40.User();
                user.setUserName(UcType.newUString_UINT1(ptr.getIp()));
                user.setUserType(UcType.newUINT1(4));
                msg.getUserNum().add(user);

            }
            tx.commit();
            for (x40 m : msgs) {
                System.out.println(JSONObject.fromObject(m.toJSON()).toString(4));
                System.out.println(HexUtil.getHex(m.toBytes()));
                String comment = "【"+groupBean.getId()+"("+groupBean.getName()+")"+"】【brasip】【＋】【"+brasips.size()+"】";
                PolicySupport.getInstance().getApi().addPolicy("0x40", groupBean.getInstanceName(), m.toJSON(), comment);
            }
//            out.println("{\"result\":\"true\"}");
        } catch (Exception e) {
            e.printStackTrace();
//            out.println("{\"result\":\"false\"}");
            tx.rollback();
        } finally {
//            out.close();
            session.close();
        }


    }
}
